home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18406 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  49 lines

  1. Path: news.th-darmstadt.de!news
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A question of privacy
  5. Date: 20 Apr 1996 10:23:24 +0200
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Sender: enno@kitz.inferenzsysteme.informatik.th-darmstadt.de
  8. Message-ID: <lt68avmhhf.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <3177F514.544E@cstar.ac.com>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: Asgeir Olafsson's message of Fri, 19 Apr 1996 15:18:28 -0500
  12. X-Newsreader: Gnus v5.1
  13.  
  14. In article <3177F514.544E@cstar.ac.com> Asgeir Olafsson <olafsson@cstar.ac.com> writes:
  15.  
  16.    Should the following work according to the ANSI C++
  17.    standard draft?
  18.  
  19.    // Compiles fine under VC++ 4.0
  20.    class A {
  21.    private:
  22.        A() {}
  23.    };
  24.  
  25.    class B {
  26.    public:
  27.        A &f() { return A();} // No complaints about privacy
  28.    };
  29.  
  30.    void g()
  31.    {
  32.        B b;
  33.        A a = b.f();
  34.    }
  35.  
  36.  
  37.    I suspect what is happening, is that a temporary object
  38.    is being created, so if this is legal the object could be
  39.    deleted as soon as "a" goes out of scope?
  40.  
  41. The compiler should complain about the private constructor of class 'A'.
  42. Anyway if you make A's ctor public, you'll return a reference to an
  43. object with a lifetime bounded to the member-function call. That is,
  44. it's not guaranteed that following copy-construction 'A a = b.f();' will
  45. work properly.
  46.  
  47.         Enno
  48.  
  49.